/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

#include "Matrix.h"

namespace OptixDotNet 
{
	ref class Group;
	ref class GeometryGroup;
	ref class Selector;

	/// <summary>
	/// Defines the Matrix layout used by Optix: Row major or Column major
    /// </summary>
	public enum class MatrixLayout
	{
		ColumnMajor = 0,
		RowMajor	= 1
	};

	/// <summary>
	/// A hierarchy node that geometrically transforms rays, in order to transform geometric objects.
	/// A Transform node can contain a: <see cref="OptixDotNet::Group">Group</see>, <see cref="OptixDotNet::GeometryGroup">GeometryGroup</see>, 
	/// <see cref="OptixDotNet::Selector">Selector</see>, or <see cref="OptixDotNet::Transform">Transform</see>.<br/>
	/// Transforms are RowMajor by default. You must set their <see cref="MatrixLayout"/> to ColumnMajor before setting the transform's matrix.
    /// </summary>
	public ref class Transform : public ContainerNode
	{
	public:

		/// <summary>
		/// Creates a Transform Node
        /// </summary>
		Transform( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			CheckError( rtTransformValidate( mTransform ) );
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			if( mTransform )
				CheckError( rtTransformDestroy( mTransform ) );
			mTransform = 0;
		}

		/// <summary>
		/// Sets the child node for a transform
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property ContainerNode^ Child
		{
			ContainerNode^ get()
			{
				RTobject object;
				CheckError( rtTransformGetChild( mTransform, &object ) );

				RTobjecttype type;
				CheckError( rtTransformGetChildType( mTransform, &type ) );

				return ConstructContainerNodeFromType( (Type)type, object );
			}

			void set( ContainerNode^ node )
			{
				CheckError( rtTransformSetChild( mTransform, node->ObjectPtr() ) );
			}
		}

		property OptixDotNet::MatrixLayout MatrixLayout;

		/// <summary>
		/// Sets an affine transformation matrix, that will transform all child geometry of the transform node
        /// </summary>
		property OptixDotNet::Math::Matrix4x4 Matrix
		{
			OptixDotNet::Math::Matrix4x4 get()
			{
				OptixDotNet::Math::Matrix4x4 matrix;
				CheckError( rtTransformGetMatrix( mTransform, (int)this->MatrixLayout, (float*)&matrix, 0 ) );

				return matrix;
			}
			void set( OptixDotNet::Math::Matrix4x4 matrix )
			{
				CheckError( rtTransformSetMatrix( mTransform, (int)this->MatrixLayout, (float*)&matrix, 0 ) );
			}
		}

	internal:
		Transform( Context^ context, RTtransform transform );
		inline RTtransform InternalPtr() { return mTransform; }
		virtual RTobject ObjectPtr() override { return InternalPtr(); }

	private:

		RTtransform	mTransform;
	};
}